<template>
  <div class="PlayBottm" v-if="playList.length > 0">
    <div class="left">
      <img :src="playList[playIndex].al.picUrl" alt @click="show = true" />
      <div>
        <div v-text="playList[playIndex].name"></div>
        <div>横滑可以切换上下首噢</div>
      </div>
    </div>
    <div class="rigth">
      <div @click="play">
        <svg class="icon" aria-hidden="true" v-if="palyflag">
          <use xlink:href="#icon-bofang1" />
        </svg>
        <svg class="icon" aria-hidden="true" v-if="!palyflag">
          <use xlink:href="#icon-weibiaoti--" />
        </svg>
      </div>
      <div>
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-24gf-playlistMusic" />
        </svg>
      </div>
    </div>
    <audio
      ref="audio"
      :src="'https://music.163.com/song/media/outer/url?id=' + playList[playIndex].id + '.mp3 '"
    ></audio>
    <play-music :currentime="currentime" :id="playList[playIndex].id" @sonpaly="play" :palyflag="palyflag" @flashow="show = false" v-show="show" :playIndex="playIndex" :playList="playList"></play-music>
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex'
import playMusic from '../components/playMusic.vue'
export default {
  components: {
    playMusic
  },
  data () {
    return {
      palyflag: true,
      // audioSrc : 'https://music.163.com/song/media/outer/url?id='+playList[playIndex].id+'.mp3 ',
      show: false,
      // paused:false,
      currentime:'',
      intervalid:'',
    }
  },
  computed: {
    ...mapState(['playList', 'playIndex'])
  },
  watch: {
    show(){
      this.$store.commit('setsearchtopshow',this.show)
    },
    playIndex () {
      setTimeout(() => {
        this.play()
      }, 0)
      // console.log(this.$store.state.playList[this.$store.state.playIndex].id)
    },
    playList () {
      console.log( this.$store.state.playList )

    }
  },
  created () {
    // this.playList = this.$store.state.playList
    // console.log(playList)
  },
  methods: {
    updateTime(){
      this.intervalid = setInterval(() => {
        // console.log(this.$refs.audio.currentTime)
        this.currentime = this.$refs.audio.currentTime
        if(this.$refs.audio.ended){
          // console.log(this.$refs.audio.ended)
          this.playIndex<this.playList.length?this.$store.commit('setpalyIndex', this.playIndex+1):''
        }
      }, 1000);
    },
    
    play () {
      if (this.$refs.audio.paused) {
        this.$refs.audio.play()
        this.updateTime()
        this.palyflag = false
      } else {
        this.$refs.audio.pause()
        this.palyflag = true
        window.clearInterval(this.intervalid)
      }

    }
  }
}
</script>

<style lang="less" scoped>
.PlayBottm {
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding: 0 0.1rem;
  background-color: #fff;
  padding: 0.2rem 0;
  .left {
    display: flex;
    margin-left: 0.1rem;
    img {
      width: 0.8rem;
      height: 0.8rem;
      border-radius: 100%;
    }
    div {
      margin-left: 0.1rem;
      div:nth-child(2) {
        color: #ccc;
      }
    }
  }
  .rigth {
    display: flex;
    div {
      .icon {
        width: 0.5rem;
        height: 0.5rem;
        margin: 0.15rem 0.2rem;
        // margin: 0.2rem;
      }
    }
  }
}
</style>